package com.stqf.scala.spark;

import com.stqf.scala.domain.dto.AreaDTO;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created By admin
 * Date: 2020/5/8
 * Description:
 */
public class SparkSql {

    public static void main(String[] args) throws Exception {

        AreaDTO _1area = AreaDTO.of(1, "610000", "成都");
        AreaDTO _2area = AreaDTO.of(2, "620000", "绵阳");
        AreaDTO _3area = AreaDTO.of(3, "630000", "自贡");
        List<AreaDTO> areas = new ArrayList<>(Arrays.asList(_1area, _2area, _3area));

        SparkSession session = SparkSession.builder().appName("SparkSQL").master("local[*]").getOrCreate();

        Dataset<Row> dataset = session.createDataFrame(areas, AreaDTO.class);
        dataset.createOrReplaceTempView("area");

        dataset.show();

        session.sql("select count(*) total from area").show();

        session.sql("select id,name,code from area order by id asc").show();

        session.sql("select id,name,code from area order by code desc").show();

        session.close();

    }

}
